<?php
/**
 * 教职工信息控制器
 * @author GL
 * @date 2018.8.24
 */
namespace App\Http\Controllers\Crm;

use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;
use App\Model\Crm\UserModel;
use App\Model\Crm\WorkerModel;
use session;
use illuminate\support\facades\route;
use Illuminate\Support\Facades\Cookie;

class WorkerController extends BaseController{

    //教职工信息
    public function workerList(Request $request)
    {
        dd(session('userInfo'));
        //搜索条件类型
        $type = addslashes($request->get('type', 0));
        $name = addslashes($request->get('name'));

        $where['user.is_del'] = 0;
        if($type == 1 && !empty($name)){

            $groupWhere[] = ['name', 'like', '%' . $name . '%'];
            $res = WorkerModel::getGroupId($groupWhere);
            if($res){
                $ids = array_column($res,'id');
                $where[] = [function($query) use ($ids){
                    $query->whereIn('info.department', $ids);
                }];
            }

        }elseif ($type == 2 && !empty($name)){

            $where[] = ['info.name', 'like', '%' . $name . '%'];

        }elseif ($type == 3 && !empty($name)){

            $object = config('teacher.teacher_object');
            $ids = [];
            foreach ($object as $k=>$v){
                if(strstr($v, $name)){
                    $ids[] = $k;
                }
            }

            if($ids){
                $where[] = [function($query) use ($ids){
                    $query->whereIn('info.department', $ids);
                }];
            }

        }elseif ($type == 4 && !empty($name)){

            $where[] = ['info.staff_no', 'like', '%' . $name . '%'];

        }elseif ($type == 5 && !empty($name)){

            $id = $name  == '男' ? 1 : 2;
            $where['info.sex'] = $id;

        }elseif ($type == 6 && !empty($name)){

            $where[] = ['info.card', 'like', '%' . $name . '%'];

        }elseif ($type == 7 && !empty($name)){

            $where[] = ['info.mobile', 'like', '%' . $name . '%'];

        }elseif ($type == 8 && !empty($name)){

            $object = config('teacher.teacher_situation');
            $ids = [];
            foreach ($object as $k=>$v){
                if(strstr($v, $name)){
                    $ids[] = $k;
                }
            }

            if($ids){
                $where[] = [function($query) use ($ids){
                    $query->whereIn('info.teacher_situation', $ids);
                }];
            }

        }

        $field = ['info.user_id', 'info.staff_no', 'info.name', 'info.sex', 'info.card', 'info.mobile', 'info.teacher_situation', 'info.teacher_object', 'info.department'];

        $workerList = WorkerModel::getWorkerList($where, $field);
        dd($workerList->all());

    }


    //教职工信息
    public function workerInfo(Request $request)
    {
        return view('/crm/worker/info');
    }

}